<?php
namespace Home\Controller;
class IndexController extends BaseController {

    public function index(){
       $cate=M('cate')->field('id,cate_name')->where(array('fid'=>0))->select();
       $this->assign('cates',$cate);
       //读取商品信息
       $res=M('goods_cate_type')->where(array('cate_id'=>2,'status'=>1))->select();
       $this->assign('phone',$res);
       //智能硬件
       $res=M('goods_cate_type')->where(array('cate_id'=>39,'status'=>1))->select();
       $this->assign('computer',$res);
       $this->display();
    }
    public function docate(){
        $data=M('cate')->where(array('fid'=>I('post.id')))->select();
        $this->ajaxReturn($data);  
    }
    public function goods(){
        //读取顶级分类
        $cate=M('cate')->field('id,cate_name')->where(array('fid'=>0))->select();
        $this->assign('cates',$cate);
        //读取二级分类
        $c=M('cate')->field('cate_name')->where(array('id'=>I('get.id')))->find();
        $this->assign('cate',$c);
        //读取商品信息
        $res=M('goods')->where(array('cate_id'=>I('get.id'),'status'=>1))->select();
        $this->assign('goods',$res);
        //查询类型表
        $type=M('type')->where(array('id'=>I('get.type')))->find();
        $this->assign('type',$type);
        $this->display();
    }
    public function detail(){
        $id=I('get.id');
        $cate_name=M('goods_brand')->field('顶级分类,二级分类')->where(array('id'=>$id))->find();
        $this->assign('fcate',$cate_name);
        $cate=M('cate')->field('id,cate_name')->where(array('fid'=>0))->select();
        $this->assign('cates',$cate);
        $good=M('goods')->where(array('id'=>I('get.id')))->find();
        $data=explode(';',$good['goods_attr_pic']);
        $this->assign('attr_pic',$data);
        $this->assign('goods',$good);
        //商品类型id到商品总属性表查询 得到自增id 然后去商品属性表查询 商品属性
        $attrid=M('attr_type')->where(array('type_id'=>I('get.type')))->select();
        //读取属性分类
        $color=M('goods_attr_g_t')->where(array('goods_id'=>I('get.id'),'attr_id'=>$attrid[0]['id']))->select();
        $val=M('goods_attr_g_t')->where(array('goods_id'=>I('get.id'),'attr_id'=>$attrid[1]['id']))->select();
        $mem=M('goods_attr_g_t')->where(array('goods_id'=>I('get.id'),'attr_id'=>$attrid[2]['id']))->select();
        $this->assign('color',$color);
        $this->assign('val',$val);
        $this->assign('mem',$mem);
        //读取评论表使用递归
        $data=M('comment')->where(array('goods_id'=>I('get.id'),'check'=>1))->order('fid asc')->select();
        $list=BaseController::tree($data);
        $this->assign('comments',$list);
        $this->display();
    }
    public function addcart(){
      $re=M('ptype')->where(array('id'=>I('post.id'),'user'=>I('post.user')))->find();  
       $arr=array();
       if($re){
           $arr['status']=0;
           $arr['msg']='已经添加过该商品,请勿重复添加';
       }else{
          $data=M('ptype')->add(I('post.'));  
          if($data){
              $arr['status']=1;
              $arr['msg']='添加成功';
          } 
       }
       $this->ajaxReturn($arr);
    }
}